package com.necer.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;


public abstract class BaseFragment<VB extends ViewBinding> extends Fragment {

    protected View rootView;
    protected VB binding;

    protected Context context;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = createBinding(inflater, container);
        rootView = binding.getRoot();
        return rootView;
    }

    public abstract VB createBinding(LayoutInflater inflater, ViewGroup container);

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getAttachActivity();
        onCreated();
    }

    protected abstract void onCreated();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    public <V extends View> V findViewById(@IdRes int id) {
        return rootView == null ? null : rootView.findViewById(id);
    }

    public Activity getAttachActivity() {
        return getActivity();
    }


}